<?php

namespace yak\layui\search;

use Yii;
use yak\layui\widgets\grid\LayuiActionColumn;
use yak\layui\widgets\grid\LayuiCheckColumn;
use yak\layui\traits\SearchTrait;
use yak\layui\models\rbac\AuthItem;
use yii\data\ActiveDataProvider;

/**
 * AuthItemSearch represents the model behind the search form about `yak\layui\models\rbac\AuthItem`.
 */
class AuthItemSearch extends AuthItem implements LayuiSearchInterface
{
    use SearchTrait;

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['name', 'description', 'rule_name', 'data'], 'safe'],
            [['type'], 'integer'],
            [['yak_lst', 'yak_lst_txt', 'yak_lst_ext', 'yak_dt', 'yak_dt_s', 'yak_dt_e'], 'safe'],
            [['date_s', 'date_e', 'date_sel', 'yak_output_excel'], 'safe']
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return AuthItem::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = AuthItem::find();

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params, '');

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        if (!empty($this->yak_dt_s) && !empty($this->yak_dt_e)) {
            $this->yak_dt_e = date('Y-m-d', strtotime('+1 day', strtotime($this->yak_dt_e)));
            $query->andFilterWhere(['between', $this->yak_dt, $this->yak_dt_s, $this->yak_dt_e]);
        }

        if (!empty($this->yak_lst)) {

            if ($this->yak_lst_ext == 'EQ') {
                $query->andFilterWhere([$this->yak_lst => $this->yak_lst_txt]);
            }

            if ($this->yak_lst_ext == 'LIKE') {
                $query->andFilterWhere(['like', $this->yak_lst, $this->yak_lst_txt]);
            }
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'type' => $this->type,
            'created_at' => $this->created_at,
            'updated_at' => $this->updated_at,
        ]);

        $query->andFilterWhere(['like', 'name', $this->name])
            ->andFilterWhere(['like', 'description', $this->description])
            ->andFilterWhere(['like', 'rule_name', $this->rule_name])
            ->andFilterWhere(['like', 'data', $this->data]);

        return $dataProvider;
    }

    /**
     * @return array
     */
    public function columns(): array
    {
        return [
            [
                'class' => LayuiCheckColumn::class
            ],
            'name',
            [
                'attribute' => 'type',
                'format' => ['selected', $this->typeLabels()]
            ],
            'description',
            'rule_name',
            'data',
            'created_at:datetime',
            'updated_at:datetime',
            [
                'class' => LayuiActionColumn::class,
                'toolbar' => '#tool'
            ]
        ];
    }

    /**
     * @return array
     */
    public function limits(): array
    {
        // TODO: Implement limits() method.
        return [10, 20, 50, 100];
    }

    /**
     * @return int
     */
    public function pageSize(): int
    {
        // TODO: Implement pageSize() method.
        return 15;
    }
}
